clear all
macro drop _all

/*put directory information here:*/
cd ""
use "Panel_Replication_label.dta", clear

capt prog drop my_ptest
*! version 1.0.0  14aug2007  Ben Jann
program my_ptest, eclass
*clus(clus_var)
syntax varlist [if] [in], by(varname) clus_id(varname numeric) strat_id(varname numeric) [ * ] /// clus_id(clus_var)  

marksample touse
markout `touse' `by'
tempname mu_1 mu_2 mu_3 se_1 se_2 se_3 d_p
capture drop TD*
tab `by' , gen(TD)
foreach var of local varlist {
 reg `var' TD1 TD2 i.`strat_id' `if', nocons vce(cluster `clus_id')
 test (_b[TD1]- _b[TD2]== 0)
 mat `d_p'  = nullmat(`d_p'),r(p)
 matrix A=e(b)
 lincom (TD1-TD2)
 mat `mu_3' = nullmat(`mu_3'), A[1,2]-A[1,1]
 mat `se_3' = nullmat(`se_3'), r(se)
 sum `var' if TD1==1 & e(sample)==1
 mat `mu_1' = nullmat(`mu_1'), round(r(mean),0.01)
 mat `se_1' = nullmat(`se_1'), r(sd)
 sum `var' if TD2==1 & e(sample)==1
 mat `mu_2' = nullmat(`mu_2'),round(r(mean),0.01)
 mat `se_2' = nullmat(`se_2'), r(sd)
}
foreach mat in mu_1 mu_2 mu_3  se_1 se_2 se_3 d_p {
 mat coln ``mat'' = `varlist'
}
 local cmd "my_ptest"
foreach mat in mu_1 mu_2 mu_3  se_1 se_2 se_3  d_p {
 eret mat `mat' = ``mat''
}
end




global savings nsb_mtransactions nsb_deposits nsb_flow mtransactions msavings_bank  msavings_nonbank msavings_total	
global RHSvars "female education SE agemployment hhexpenditure beta delta digitspan in_seetu  formalaccount_alt formalbalance"
global table3_rhs "wage_emp ag_SE SE mwage_pay_total mag_income_total mbusiness_income mwage_hours_worked hours_worked"
global table4_rhs "wage_emp ag_SE SE mwage_pay_total mag_income_total mbusiness_income mwage_hours_worked hours_worked"
global table6_rhs "mhhincome_total  wage_emp SE bus_inc consumption  cs_sin   blue_red_frac   msavings_bank msavings_nonbank msavings_total"
global table7_rhs "mhhincome_total  income_earned    consumption mtransfers_tospouse mtransfers_fromspouse  mnet_transfers msavings_bank msavings_nonbank msavings_total"
global table8_rhs "mseetus_number mseetu_saved mtransfers_fromoutside mtransfers_tooutside msavings_total"
global balance "mseetus_number mseetus_payout SE masset_purchase  inventory_value mbusiness_expense mbusiness_sales mbusiness_income nonnsb_mtransactions mseetu_saved mseetu_withdrew  mall_saved mall_withdrew mcashbalance_change mloans_received mincome_agg mcons_total mhhincome_earned mhhincome_total mpersincome_withdraw mtransfers_fromspouse mtransfers_fromoutside mpersincome_total mtransfers_tospouse mtransfers_tokids mtransfers_toall msavings_bank msavings_cash msavings_seetu msavings_other msavings_total"
global formal_credit = "mloangov_received mloanmfi_received  mloanbank_received mloans_received cind_loans"
global ind_consumption = "cind_tobacco cind_alcohol cind_parties cind_gambling cind_total"
global transfers " mtransfers_toall mtransfers_fromspouse mtransfers_fromoutside minfloans_given minfgifts_given minfloans_received minfgifts_received"                             

encode zone, gen(zoneid)

*---------------*
*Determinants of Uptake and Usage*
*---------------*
/*Table 1*/
preserve
/*keeping only those individuals who were offered the treatment:*/
drop if treat==0
egen deposit_total = sum(nsb_deposits), by(sheno)
egen withdrawals_total= sum(nsb_withdrawals), by(sheno)
gen deposit_any = (deposit_total>0 & deposit_total!=.)
gen withdrawal_any = (withdrawals_total>0 & withdrawals_total!=.)
egen sdeposit = sum(nsb_deposits_all), by(sheno)
egen swithdrawals = sum(nsb_withdrawals_all), by(sheno)
egen sumtransactions = sum(nsb_mtransactions), by(sheno)
gen balance = sdeposit - swithdrawals
gen lbalance = log(balance)
gen ldeposit = ln(deposit_total)
gen lwithdrawal = ln(withdrawals_total)
replace hhexpenditure = hhexpenditure/1000000
replace totalbalance = totalbalance/1000000
replace formalbalance = formalbalance/1000000
gen agesq = age^2
gen female_hhhead = female * hh_head
gen female_married = female * married
/*Round 1-5 values of RHS variables only:*/
/*note that hhexpenditure and formal balances still come from the baseline survey*/
keep if round>=1 & round<=5
/*one observation per person:*/
collapse  deposit_any withdrawal_any sumtransactions ldeposit lwithdrawal lbalance $RHSvars, by(sheno)
/*Uptake analysis:*/
eststo clear
eststo: regr deposit_any  $RHSvars, robust
estadd ysumm
eststo: regr withdrawal_any  $RHSvars, robust
estadd ysumm
eststo: regr sumtransactions  $RHSvars, robust
estadd ysumm
eststo: regr ldeposit $RHSvars, robust
estadd ysumm
eststo: regr lwithdrawal $RHSvars, robust	
estadd ysumm
eststo: regr lbalance $RHSvars, robust
estadd ysumm	
estout using "Results/Tabla1.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep($RHSvars)  stats(N ymean r2 , fmt(a2 a2 a2 ) labels ("Observations"  "Mean of dep. var. in sample" "\$R^2\$" )) replace

esttab using "Results/Tabla1.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep($RHSvars)  stats(N ymean r2 , fmt(a2 a2 a2 ) labels ("Observations"  "Mean of dep. var. in sample" "\$R^2\$" )) replace
restore



*---------------*
*Savings Effects*
*---------------*
/*Table 2*/
/*Savings impact, truncating right tail at 99%:*/
preserve

foreach x in $savings{
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
egen p01_nsb_flow = pctile(nsb_flow), p(01)
replace nsb_flow = p01_nsb_flow if nsb_flow<p01_nsb_flow

replace nsb_flow = 0 if nsb_flow==.
replace nsb_deposits = 0 if nsb_deposits==.
replace nsb_mtransactions = 0 if nsb_mtransactions==.

eststo clear
foreach x in $savings{
	eststo: cgmreg `x' treated i.round i.id if round>1 & round<=18 & panel==1,  cluster(id zone)
	estadd ysumm
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	
}
estout using "Results/Table2_PanelA.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table2_PanelA.csv" , style(tab) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace


eststo clear
foreach x in $savings{
	eststo: cgmreg `x' treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20 i.round i.id if round>1 & panel==1, cluster(id zone)
	estadd ysumm
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}
estout using "Results/Table2_PanelB.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table2_PanelB.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

restore	



*------------------*
*Aggregated Headwaters Effects*
*------------------*
/*Table 3*/
/*headwaters of savings, truncating right tail at 99%:*/
eststo clear
foreach x in $table3_rhs{
	eststo: cgmreg `x' treated i.round i.id if round>1 & round<=18 & panel==1, cluster(id zone)
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}
estout using "Results/Table3_PanelA.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table3_PanelA.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

	
preserve
foreach x in    $table3_rhs{
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}

eststo clear
foreach x in $table3_rhs{
	eststo: cgmreg `x' treated i.round i.id if round>1 & round<=18 & panel==1, cluster(id zone)
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}	
estout using "Results/Table3_PanelB.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table3_PanelB.csv" , style(tab)  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

eststo clear	
foreach x in $table3_rhs{
	eststo: cgmreg `x' treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20 i.round i.id if round>1 & panel==1,  cluster(id zone)
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}
estout using "Results/Table3_PanelC.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table3_PanelC.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

restore


*---------------------------------------*
*Why do incomes increase from treatment?*
*---------------------------------------*
/*Tables 4 and table 5 */

preserve
foreach x in   $table4_rhs{
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
	replace `x'=0 if `x'==.
}

/*Simple treatment dummy:*/
/*Household income among everybody:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'  treated  i.round i.id  if round>1 & round<=18 & panel==1, cluster(id zone)
}
estout using "Results/Table4_PanelA.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table4_PanelA.csv" ,starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace


/*Household income: some SE and some "manufacturing" during baseline:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'  treated  i.round i.id  if round>1 & round<=18 & panel==1  & any_sector1_base==1 & any_SE_base==1, cluster(id zone)
}
estout using "Results/Table4_PanelB.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table4_PanelB.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

/*Some SE but no manufacturing at baseline:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'  treated  i.round i.id  if round>1 & round<=18 & panel==1  & any_sector1_base==0 & any_SE_base==1, cluster(id zone)
}
estout using "Results/Table4_PanelC.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2 ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table4_PanelC.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2 ) labels ("Observations"  "\$R^2\$" )) replace

/*No SE druing baseline:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'  treated  i.round i.id  if round>1 & round<=18 & panel==1  & any_SE_base==0, cluster(id zone)
}
estout using "Results/Table4_PanelD.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table4_PanelD.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep(treated)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace




/*By period:*/
/*Household income among everybody:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'   treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20  i.round i.id  if round>1 & panel==1, cluster(id zone)
}
estout using "Results/Table5_PanelA.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table5_PanelA.csv" , style(tab) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

/*Household income among the Not always Baseline SE:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'   treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20  i.round i.id  if round>1 & panel==1  & any_sector1_base==1 & any_SE_base==1, cluster(id zone)
}
estout using "Results/Table5_PanelB.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table5_PanelB.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace


/*Focusing now on the SE_always group:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'   treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20  i.round i.id  if round>1 & panel==1  & any_sector1_base==0 & any_SE_base==1, cluster(id zone)
}
estout using "Results/Table5_PanelC.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table5_PanelC.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

/*No SE druing baseline:*/
eststo clear
foreach var of varlist $table4_rhs{
eststo: cgmreg `var'   treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20  i.round i.id  if round>1 & panel==1  & any_SE_base==0, cluster(id zone)
}
estout using "Results/Table5_PanelD.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

esttab using "Results/Table5_PanelD.csv" , style(tab)  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20)  stats(N  r2 , fmt(a2 a2  ) labels ("Observations"  "\$R^2\$" )) replace

restore


*---------------*
*CONSUMPTION AND SIN GOODS:*
*Heterogeneity by Hyperbolicity:*
*---------------*
/*Table 6*/
/*truncating right tail at 99%:*/

preserve
foreach x in  $table6_rhs {
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
eststo clear
foreach x in  $table6_rhs{
	eststo: cgmreg `x'  treated_incon treated  i.round i.id if round>1 &  round<=18 & panel==1, cluster(id zone)
	estadd ysumm
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	}	
estout using "Results/Table6.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_incon treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table6.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_incon treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

	
sum  mhhincome_total  wage_emp SE bus_inc consumption  cs_sin   blue_red_frac   msavings_bank msavings_nonbank msavings_total if round>1 & round<=18 & panel==1 & treat==0	
restore	

preserve
foreach x in  $table6_rhs {
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
eststo clear
foreach x in  $table6_rhs{
	eststo: cgmreg `x'  treated_incon treated  i.round i.id if round>1 & panel==1, cluster(id zone)
	estadd ysumm
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	}	
estout using "Results/Table6_20.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_incon treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace
	
esttab using "Results/Table6_20.csv" ,  starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_incon treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace
	
sum  mhhincome_total  wage_emp SE bus_inc consumption  cs_sin   blue_red_frac   msavings_bank msavings_nonbank msavings_total if round>1 & round<=18 & panel==1 & treat==0	
restore	
	
*---------------------------------------*
*Seetu Saturation Effects:*
*---------------------------------------*
/*Table 7*/
preserve
keep if seetu_sat!=.

foreach x in mtransfers_fromoutside mtransfers_tooutside msavings_total {
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}	
eststo clear
foreach x in  $table7_rhs{
	eststo: cgmreg `x' treated  i.round i.id if round>1 &  round<=18 & panel==1 & seetu_sat!=.,  cluster(id zone)
	estadd ysumm
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	eststo: cgmreg `x'   treated  seetu_sat_t i.round i.id if round>1  &  round<=18 & panel==1 & seetu_sat!=.,  cluster(id zone)
	estadd ysumm
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}

estout using "Results/Table7.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated seetu_sat_t)  stats(N hh_numb r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "\$R^2\$" )) replace

esttab using "Results/Table7.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated seetu_sat_t)  stats(N hh_numb r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "\$R^2\$" )) replace
	
restore

*---------------*
*TABLE 8: Heterogeneity by Respondent Income Share*
*---------------*
preserve

foreach x in $table8_rhs {
                egen p99_`x' = pctile(`x'), p(99)
                replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
                }
 
/*Interactions using Terciles:*/
eststo clear
foreach x in $table8_rhs {
        eststo: cgmreg `x' treated perst2_treated perst3_treated  i.round i.id if round>1 & round<=18 & panel==1, nocons cluster(id zone)
	estadd ysumm
	qui tab id if e(sample)==1
}    
estout using "Results/Table8.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated perst2_treated perst3_treated)  stats(N hh_numb r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "\$R^2\$" )) replace

esttab using "Results/Table8.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated perst2_treated perst3_treated)  stats(N hh_numb r2 , fmt(a2 a2 a2 ) labels ("Observations" "Number of HHs" "\$R^2\$" )) replace
     
restore
	

	
/******************************************************APPENDIX TABLES:*****************************************************/	
	
	
*---------------*
*Attrition Tests*
*---------------*
/*Table A2*/
preserve
foreach x in mcons_total msavings_bank mloans_received {
	replace `x' = `x'/100000
	}
keep if (round>=1 & round<=5)
collapse attrite_alt attrite treat female education digitspan beta delta SE ag_SE mcons_total  mseetus_number msavings_bank  mloans_received    zoneid , by(sheno)
eststo clear
eststo:  regr attrite treat, cluster(zoneid)
estadd ysumm
eststo:  regr attrite treat female education digitspan beta delta SE ag_SE mcons_total  mseetus_number msavings_bank   , cluster(zoneid)
estadd ysumm
eststo:  regr attrite_alt treat, cluster(zoneid)
estadd ysumm
eststo:  regr attrite_alt treat female education digitspan beta delta SE ag_SE mcons_total  mseetus_number msavings_bank   , cluster(zoneid)
estadd ysumm
estout using "Results/Table_2A.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
stats(N ymean r2 , fmt(a2 a2 a2 ) labels ("Observations" "Mean Dep. Var." "\$R^2\$" )) replace

esttab using "Results/Table_2A.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
stats(N ymean r2 , fmt(a2 a2 a2 ) labels ("Observations" "Mean Dep. Var." "\$R^2\$" )) replace
restore



*---------------*
*Balance Tests*
*---------------*
/*Table A3:*/
/*Balance tests, using rounds 2-5 and truncating right tail at 99%*/
preserve
foreach x in $balance{
	egen p99_`x' = pctile(`x'), p(99)
	*replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
eststo clear
xi: my_ptest  mtransactions female delta beta $balance  in_seetu_panel if round>=2 & round<=5 & SE_ever==1, by(treat) clus_id(zoneid) strat_id(round)

esttab using "Results/Table_3A_1.tex", label replace ///
booktabs nomtitle nonumbers noobs nodep star(* 0.10 ** 0.05 *** 0.01)  collabels("Control" "Treatment" "Treatment - Control")  ///
cells("mu_1(fmt(%12.2fc)) mu_2(fmt(%12.2fc)) mu_3(fmt(a2) star pvalue(d_p))" "se_1(par) se_2(par) s3_2(par)") 

esttab using "Results/Table_3A_1.csv", label replace ///
nomtitle nonumbers noobs nodep star(* 0.10 ** 0.05 *** 0.01)  collabels("Control" "Treatment" "Treatment - Control")  ///
cells("mu_1(fmt(a2)) mu_2(fmt(a2)) mu_3(fmt(a2) star pvalue(d_p))" "se_1(par) se_2(par) s3_2(par)") 

restore
	
	

/*Balance tests using R1 data:*/
preserve	
foreach x in $RHSvars{
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
eststo clear
eststo:  xi: my_ptest  female if round==1 & panel==1 , by(treat) clus_id(zoneid) strat_id(round)
eststo:  xi: my_ptest  $RHSvars lhhexpenditure if round==1 & panel==1 & female==1, by(treat) clus_id(zoneid) strat_id(round)
esttab using "Results/Table_3A_2.tex", label replace ///
booktabs nomtitle nonumbers noobs nodep star(* 0.10 ** 0.05 *** 0.01)  collabels("Control" "Treatment" "Treatment - Control")  ///
cells("mu_1(fmt(%12.2fc)) mu_2(fmt(%12.2fc)) mu_3(fmt(a2) star pvalue(d_p))" "se_1(par) se_2(par) s3_2(par)") 

esttab using "Results/Table_3A_2.csv", label replace ///
nomtitle nonumbers noobs nodep star(* 0.10 ** 0.05 *** 0.01)  collabels("Control" "Treatment" "Treatment - Control")  ///
cells("mu_1(fmt(a2)) mu_2(fmt(a2)) mu_3(fmt(a2) star pvalue(d_p))" "se_1(par) se_2(par) s3_2(par)") 
restore


*------------------*
*Headwaters Effects*
*------------------*
/*Table A4*/
/*headwaters of savings, truncating right tail at 99%:*/

preserve

/*don't trim inf_savings because too many variables have infrequent non-zeros, then drop when trimmed:*/
foreach x in    $formal_credit $ind_consumption $transfers{
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
}
foreach x in   $inf_savings  $formal_credit  $ind_consumption $transfers total_cash_balance{
	replace `x'=0 if `x'==.
}


/*four-month windows:*/
eststo clear
foreach x in  $inf_savings  $formal_credit  $ind_consumption $transfers total_cash_balance {
	eststo: areg `x' treated i.round if round>1 & round<=18 & panel==1, absorb(id) vce(cluster id)
	estadd ysumm
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	
}	
estout using "Results/Table_4A_LeftPanel.tex" , keep(treated) style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none) stats(N hh_numb ymean r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "HHs" "Mean Dep. Var." "\$R^2\$" ))  replace
esttab using "Results/Table_4A_LeftPanel.csv" , keep(treated) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none) stats(N hh_numb ymean r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "HHs" "Mean Dep. Var." "\$R^2\$" ))  replace

eststo clear
foreach x in  $inf_savings  $formal_credit  $ind_consumption $transfers total_cash_balance {
	eststo: areg `x' treat_6_9 treat_10_13 treat_14_18 treat_19 treat_20 i.round if round>1 & panel==1, absorb(id) vce(cluster id)
	estadd ysumm
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
}	
estout using "Results/Table_4A_RightPanel.tex" , keep(treat_6_9 treat_10_13 treat_14_18) style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) stats(N hh_numb ymean r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "HHs" "Mean Dep. Var." "\$R^2\$" )) replace
esttab using "Results/Table_4A_RightPanel.csv" , keep(treat_6_9 treat_10_13 treat_14_18) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) stats(N hh_numb ymean r2 , fmt(a2 a2 a2 a2 ) labels ("Observations" "HHs" "Mean Dep. Var." "\$R^2\$" )) replace

restore



*---------------*
*Heterogeneity by Female:*
*---------------*
/*Table A5*/
/*truncating right tail at 99%:*/
preserve
eststo clear
foreach x in blue_red_frac cind_total  mcons_total mtransfers_tospouse mtransfers_tokids mtransfers_toall mtransfers_fromspouse mtransfers_fromoutside  msavings_total{
	eststo:  cgmreg `x' treated_female   treated i.round i.id if round>1 & round<=18 & panel==1, cluster(id zone)
	estadd ysumm
	qui sum `x' if e(sample)==1 & treat==0
	estadd scalar ymean_control=r(mean)
	qui tab id if e(sample)==1
	estadd scalar hh_numb=r(r)
	}	
estout using "Results/Table_5A.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_female   treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

esttab using "Results/Table_5A.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treated_female   treated)  stats(N hh_numb ymean_control r2 , fmt(a2 a2 a2 a2) labels ("Observations" "Number of HHs" "Control group mean" "\$R^2\$" )) replace

restore	


	
	



	
*---------------------------------------*
*Balance of the Seetu Saturation Experiment:*
*---------------------------------------*
/*Table A6*/
preserve
keep if seetu_sat!=.
foreach x in mseetus_number mseetu_saved  mtransfers_fromoutside mtransfers_tooutside msavings_total {
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
	}
eststo clear
foreach x in mseetus_number mseetu_saved  mtransfers_fromoutside mtransfers_tooutside msavings_total {
	eststo: cgmreg `x' treat i.round  if round>=2 & round<=5 & panel==1 & seetu_sat!=., cluster(id zone)
	eststo: cgmreg `x' treat seetu_sat i.round  if round>=2 & round<=5 & panel==1, cluster(id zone)
}
estout using "Results/Table_6A.tex" , style(tex) starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat seetu_sat)  stats(N r2 , fmt(a2 a2 ) labels ("Observations" "\$R^2\$" )) replace

esttab using "Results/Table_6A.csv" , starl(* 0.10 ** 0.05 *** 0.01) label cells(b(star fmt(a2)) se(par fmt(a2))) mlabels(none) collabels(none)  ///
keep( treat seetu_sat)  stats(N r2 , fmt(a2 a2 ) labels ("Observations" "\$R^2\$" )) replace

restore




/****************************************************************************/
/*                               PICTURES                                   */
/****************************************************************************/

/*Subgroup balance Densities on rounds 2-5, comparing treatment and control:*/
/*female and male*/

preserve
gen outcome = lmpersincome_total
drop if lmpersincome<6
drop if lmpersincome>12
tw 		 kdensity outcome if female==1 & round>=1 & round<=5 & treat==1 , lwidth(thick) legend(label(1 "Female Treatment"))  || ///
	kdensity outcome if female==1 & round>=1 & round<=5 & treat==0 , lwidth(thin) legend(label(2 "Female Control"))  || ///
	kdensity outcome if female==0 & round>=1 & round<=5 & treat==1, lwidth(thick) lpattern(dash) legend(label(3 "Male Treatment")) ytitle("Density") || ///
	 kdensity outcome if female==0 & round>=1 & round<=5 & treat==0 , lwidth(thin) lpattern(dash) legend(label(4 "Male Control")) ///
		title("Within-Gender Imbalances, Pretreatment rounds 1-5") 	xtitle("Ln(Monthly Personal Income)")
		graph export "Results/Figure1.pdf", as(pdf) replace
restore




/*Picture of number of transactions per month for treatment and control:*/

preserve
drop if (round<2 ) 
drop if round>18
collapse mtransactions nonnsb_mtransactions, by(round treat)
gen t_nonnsb_trans = nonnsb_mtrans if treat==1
gen t_trans = mtrans if treat==1
gen c_trans = mtrans if treat==0
label var t_trans "Treatment, all"
label var t_nonnsb_trans "Treatment, non-NSB"
label var c_trans "Control"
sort round
line t_trans round , lwidth(thick)|| line t_nonnsb_trans round, lpattern (-) lwidth(thick) || line c_trans round, xline(5.1) ///
	lpattern(_.) title("Number of financial transactions per month") ///
	 text(2.5 4.4 "Treatment Begins", place(e))  ///
	xlabel(2 "Aug '10" 4 "Oct '10" 6 "Dec '10" 8 "Feb '11" 10 "Apr '11" 12 "Jun '11" 14 "Aug '11" 16 "Oct '11" 18 "Dec '11") 
	graph export "Results/Figure2.pdf", as(pdf) replace
restore

/*Picture of business income per month for treatment and control:*/

preserve
foreach x in  mbusiness_income {
	egen p99_`x' = pctile(`x'), p(99)
	replace `x'=p99_`x' if `x'>p99_`x' & `x'!=.
	*replace `x'=0 if `x'==.
	}
drop if round<2 
drop if round>18
keep if SE_ever==1
collapse mbusiness_income, by(round treat)
rename mbusiness_income mtrans
gen t_trans = mtrans if treat==1
gen c_trans = mtrans if treat==0
label var t_trans "Treatment"
label var c_trans "Control"
sort round
line t_trans round, lwidth(thick) || line c_trans round, xline(5.1) lpattern(_.) title("Total Business Income per Month")  ///
	 text(13500 4.4 "Treatment Begins", place(e)) ytitle("Rupees") ///
		xlabel(2 "Aug '10" 4 "Oct '10" 6 "Dec '10" 8 "Feb '11" 10 "Apr '11" 12 "Jun '11" 14 "Aug '11" 16 "Oct '11" 18 "Dec '11")
		graph export "Results/Figure3.pdf", as(pdf) replace
restore
